* Bernd Beber, Michael J. Gilligan, Jenny Guardado, and Sabrina Karim
* Code to prepare data for "Peacekeeping, International Norms, and Transactional Sex in Monrovia, Liberia"

* code below shows how variables included in the replication dataset were constructed from original questionnaire items,
* and how the subsample of observations relevant for this article was identified

* load full dataset, which is not included in the replication materials for this article
    use "Data all", clear

* variables related to transactional sex
    gen ts=s10
    _crcslbl ts s10
    gen tsfirst=s11
    _crcslbl tsfirst s11
    gen tsfrequency=s12
    replace tsfrequency="Quarterly" if tsfrequency=="Quarterly (once every 3 months)"
    _crcslbl tsfrequency s12
    gen tspayment=s13
    _crcslbl tspayment s13
    gen tsvalue=s14
    label var tsvalue "Dollar value in average week for total received for men business?"
    gen tsvaluelast=s17
    label var tsvaluelast "Last time you did men business, value of anything received?"
    gen tsUNasked=s4
    _crcslbl tsUNasked s4
    gen tsUNreceived=s5
    _crcslbl tsUNreceived s5
    gen tsUNyou=s6
    _crcslbl tsUNyou s6
    gen tsUNlast=s19
    label var tsUNlast "Last time you did men business, did the man work for the UN?"
    gen tsUNfrequency=s25
    _crcslbl tsUNfrequency s25
    gen tsforeign=s22
    label var tsforeign "Where do most foreign men who you did men business with come from?"

* variables related to UNPK
    * ever sold a good or service to UNPK
    gen PKsold=1 if regexm(q17, "Within") | q17=="Over a year ago"
    replace PKsold=0 if q17=="Never"
    label var PKsold "Ever sold a good or service to UNMIL PKO personnel"

    * ever socialized with UNPK
    gen PKsocial=1 if regexm(q19, "Within") | q19=="Over a year ago"
    replace PKsocial=0 if q19=="Never"
    label var PKsocial "Ever socialized with UNMIL PKO personnel"

    * ever spoken with UNPK
    gen PKspoke=1 if regexm(q22, "Within") | q22=="Over a year ago"
    replace PKspoke=0 if q22=="Never"
    label var PKspoke "Ever spoken with UNMIL personnel"

    * ever contacted UNPK with a problem
    gen PKproblem=1 if regexm(q35, "Once") | regexm(q35, "once") | q35=="Daily" | q35=="Weekly"
    replace PKproblem=0 if q35=="Never"
    label var PKproblem "Ever contacted UN peacekeeper with a problem"

    * has UNMIL increased city/town's safety
    gen PKsafety=1 if q24=="Yes"
    replace PKsafety=0 if q24=="No"
    label var PKsafety "Presence of UNMIL personnel has increased the city/town's safety"

    * has UNMIL increased own safety
    gen PKownsafety=1 if q25=="Yes"
    replace PKownsafety=0 if q25=="No"
    label var PKownsafety "Presence of UNMIL personnel has increased own personal security"

    * has UN military personnel made positive contribution other than security
    gen PKpositive=1 if q27!="" & q27!="Nothing" & q27!="Don't know" & q27!="Refuse"
    replace PKpositive=0 if q27=="Nothing" | q27=="Don't know"
    label var PKpositive "UN military personnel has made positive contribution other than security"

    * has UN military personnel helped solve family or interpersonal conflicts
    gen PKsolve=1 if q30=="Yes"
    replace PKsolve=0 if q30=="No"
    label var PKsolve "UN military personnel has helped solve family or interpersonal conflicts"

    * does UN military presence in city have negative consequences
    gen PKnegative=1 if q32=="Yes"
    replace PKnegative=0 if q32=="No"
    label var PKnegative "UN military presence in city has negative consequences"

    * would you like UNMIL to stay
    gen PKstay=1 if q38=="Yes"
    replace PKstay=0 if q38=="No"
    label var PKstay "Would like that UNMIL continues to stay in Liberia"

* other variables
    * age
    gen age=q1
    label var age "Age"

    * marital status
    gen married=1 if q3=="Currently married monogamously" | q3=="Currently married polygamously"
    replace married=0 if q3=="Never married" | q3=="Not married (divorced/separated)" | q3=="Not married (widowed)"
    label var married "Currently married"

    * language
    gen language=q4
    label var language "Language spoken at home"

    * cognitive tests
    gen test1=1 if q6=="Correct"
    replace test1=0 if q6=="Incorrect"
    gen test2=1 if q7=="Second"
    replace test2=0 if q7=="First" | regexm(q7, "Other")
    gen test3=1 if q8=="Correct"
    replace test3=0 if q8=="Incorrect"
    gen test4=1 if q9=="Red"
    replace test4=0 if q9=="Don't know" | regexm(q9, "Other")
    gen test5=1 if q10=="Correct"
    replace test5=0 if q10=="Incorrect"
    gen test6=1 if q11=="Correct"
    replace test6=0 if q11=="Incorrect"
    for numlist 1/6: label var testX "Passed cognitive test X"

    * religion
    gen christian=1 if q5=="Christian"
    replace christian=0 if q5!="Christian" & q5!=""
    label var christian "Christian"

    * standard of living
    encode q134, gen(enoughfood)
    encode q135, gen(enoughhousing)
    encode q136, gen(enoughhealth)
    encode q137, gen(enoughschool)
    label define enough 0 "Less than enough" 1 "Just enough" 2 "More than enough", replace
    foreach i of varlist enoughfood-enoughschool {
        recode `i' (1 5=.) (3=0) (2=1) (4=2)
        label val `i' enough
    }
    recast int enoughfood-enoughschool
    label var enoughfood "Had enough food over past month"
    label var enoughhousing "Enough housing for needs"
    label var enoughhealth "Enough health care for needs"
    label var enoughschool "Children's schooling enough for needs"

    gen electricity=1 if q143=="Yes"
    replace electricity=0 if q143=="No"
    label var electricity "Home has electricity"

    gen water=1 if q144=="Yes"
    replace water=0 if q144=="No"
    label var water "Home has running water"

    gen toilet=1 if q145=="Yes"
    replace toilet=0 if q145=="No"
    label var toilet "Home has indoor toilet"

    * economic livelihoods
    gen employee=1 if q97=="Yes"
    replace employee=0 if q97=="No"
    label var employee "Has worked as employee in previous 12 months"

    gen jobs=q98
    label var jobs "Number of jobs, if employed in previous 12 months"

    gen q101temp=q101
    replace q101temp="" if q101temp=="Don't know" | q101temp=="Refuse"
    destring q101temp, replace
    egen wages=rowtotal(q101temp q113), missing
    gen lnwages=log(wages+1)
    drop wages q101temp
    label var lnwages "Total log wages from all jobs in last month"

    gen owner=1 if q48=="Yes"
    replace owner=0 if q48=="No"
    label var owner "Owns enterprise/business or earns money working for self"

    gen businesses=q49
    label var businesses "Number of enterprises/businesses owned, if respondent owns at least one"

    gen sellforeign=1 if q60=="Yes" | q76=="Yes"
    replace sellforeign=0 if (q60=="No" & (q76=="No" | q76=="")) | q48=="No"
    label var sellforeign "Owns enterprise/business that sells to foreigners"

    foreach i of varlist q52 q68 {
        gen `i'temp=`i'
        replace `i'temp="" if `i'temp=="Don't know" | `i'temp=="Refuse"
        destring `i'temp, replace
    }
    egen earnings=rowtotal(q52temp q68temp), missing
    gen lnearnings=log(earnings+1)
    drop earnings q52temp q68temp
    label var lnearnings "Total log earnings from all enterprises in last month"

    gen savings=1 if q47=="Yes"
    replace savings=0 if q47=="No"
    label var savings "Has savings"

    foreach i of varlist q139 q141 {
        gen `i'temp=`i'
        replace `i'temp="" if `i'temp=="Don't know" | `i'temp=="Refuse"
        destring `i'temp, replace
    }
    egen assets=rowtotal(q139temp q141temp), missing
    gen lnassets=log(assets+1)
    drop assets q139temp q141temp
    label var lnassets "Total log assets"

    * hardship
    gen deaths=1 if q154=="Yes"
    replace deaths=0 if q154=="No"
    label var deaths "Household has suffered one or more deaths since start of civil war in 1999"

    gen headdeath=1 if q156=="Yes" | q163=="Yes" | q170=="Yes" | q177=="Yes" | q184=="Yes"
    replace headdeath=0 if q154=="No"
    replace headdeath=0 if q154=="Yes" & (q156=="No" | q156=="") & (q163=="No" | q163=="") & (q170=="No" | q170=="") & (q177=="No" | q177=="") & (q184=="No" | q184=="")
    label var headdeath "Head of household has died since 1999"

    gen numdeaths=q155
    replace numdeaths="" if numdeaths=="Refuse"
    destring numdeaths, replace
    label var numdeaths "Number of deaths in household since 1999"

    gen disabled=1 if q190=="Yes"
    replace disabled=0 if q190=="No"
    label var disabled "Household has suffered one or more disability or long-term illness since 1999"

    gen confiscation=1 if q221=="Yes"
    replace confiscation=0 if q221=="No"
    label var confiscation "Household has suffered confiscation or destruction of domicile since 1999"

* save subsets of data required for replication
    preserve
        * keep women aged 18-30, who were asked to complete sensitive questionnaire
        keep if q2=="Woman" & q1<31
        * drop observations where sensitive questionnaires were lost for technical (i.e. non-subject-related) reasons
        drop if s_available=="Sensitive questionnaire lost"
        * pilot surveys (conducted during the last week of June 2012) used an initial version of the questionnaire
        * all other surveys (conducted between July 25 and August 22, 2012) used the final version of the questionaire
        * for consistency, exclude surveys that used the initial version of the questionnaire
        keep if s_version=="Final"
        * this subsetting explains minor discrepancies to preliminary figures reported in the 2015 OIOS Evaluation Report
        * preliminary calculations failed to exclude a very small number of women
        * (a) who had been mistakenly asked to complete the sensitive questionnaire despite their age, or
        * (b) who were interviewed using a pilot version of the questionnaire
        keep id_hh psu pweight fpc1 fpc2 stratum ts-confiscation
        compress
        label data "Liberia survey 2012, women 18-30"
        save Female18-30, replace
    restore
    preserve
        * keep household roster information to construct age distribution by gender
        keep id_hh hh_roster id_roster r_sex* r_age*
        drop r_sex r_age r_age_diff
        compress
        label data "Liberia survey 2012, household roster"
        save HH_roster, replace
    restore
